package com.zero.magicshow.screencapture;

import android.content.Context;
import android.content.Intent;
import android.graphics.Bitmap;
import android.graphics.BitmapFactory;
import android.net.Uri;
import android.os.Bundle;
import android.support.annotation.Nullable;
import android.support.v4.app.FragmentActivity;

import android.util.Log;
import android.view.View;
import android.widget.ImageView;
import org.devio.takephoto.R;

import java.io.IOException;
import java.io.InputStream;

/**
 *
 */
public class PreviewPictureActivity extends FragmentActivity implements GlobalScreenshot.onScreenShotListener {

  public static final Intent newIntent(Context context) {
    Intent intent = new Intent(context, PreviewPictureActivity.class);
    intent.setFlags(Intent.FLAG_ACTIVITY_NEW_TASK | Intent.FLAG_ACTIVITY_CLEAR_TASK);
    return intent;
  }

  private ImageView mPreviewImageView;


  @Override
  protected void onCreate(@Nullable Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);

    setContentView(R.layout.activity_preview_layout);
    mPreviewImageView = (ImageView) findViewById(R.id.preview_image);

    GlobalScreenshot screenshot = new GlobalScreenshot(getApplicationContext());

//    Bitmap bitmap = ((ScreenCaptureApplication) getApplication()).getmScreenCaptureBitmap();
    Uri uri = getIntent().getParcelableExtra("screenshotPath");
    Bitmap bitmap = null;
// 获取传递的文件路径
    try {

      // 通过Uri获取输入流
      InputStream inputStream = getContentResolver().openInputStream(uri);
      // 将输入流转换为Bitmap
      bitmap = BitmapFactory.decodeStream(inputStream);
      // 关闭输入流
      inputStream.close();
    } catch (IOException e) {
      e.printStackTrace();
    }

    Log.e("ryze", "预览图片");
    mPreviewImageView.setImageBitmap(bitmap);
    mPreviewImageView.setVisibility(View.GONE);

    if (bitmap != null) {
      screenshot.takeScreenshot(bitmap, this, true, true);
    }

  }

  @Override
  public void onStartShot() {

  }

  @Override
  public void onFinishShot(boolean success) {
    mPreviewImageView.setVisibility(View.VISIBLE);
  }
}
